package com.lj.map.property;

import com.MapUtil;
import com.lj.map.property.base.HashMapKey;

import java.util.HashMap;

/**
 * 扩容
 *
 * defaultInitialCapacity：默认容量
 * table.length:当前实际容量
 * size：实际数据数量
 * threshold：阈值 ＝ 当前容量 ＊ loadFactor
 * loadFactor:加载因子
 * Created by yangsp1 on 16/3/31.
 */
public class ThresholdLoadfatorExt {
    public static void main(String args[]) throws Exception {
        //指定容量(2的指数，否则容量取 (n-1)*2)
        HashMap map = new HashMap(4,0.5F);

        map.put("11","11");
        System.out.println();
        System.out.println("==========第一次==========");
        MapUtil.printMapFields(map);

        HashMapKey hashMapKey1 = new HashMapKey();
        HashMapKey hashMapKey2 = new HashMapKey();
        HashMapKey hashMapKey3 = new HashMapKey();
        map.put(hashMapKey1,"");
        System.out.println();
        System.out.println("==========第二次==========");

        //超过：thrshold= 4 * 0.5 = 2 ,则开始扩容。 扩容为原来的2倍。所有位置重新计算，数据移动（性能）－－一到性hash(memcached)：长度不变，下标不变，选择算法改变
        MapUtil.printMapFields(map);
        map.put(hashMapKey2,"");
        System.out.println();
        System.out.println("==========第三次==========");
        MapUtil.printMapFields(map);
        map.put(hashMapKey3,"");

        System.out.println();
        System.out.println("==========第四次==========");
        MapUtil.printMapFields(map);

    }
}
